from django.contrib                       import messages
from django.http                          import HttpResponseRedirect,HttpResponseForbidden
from django.utils.translation             import ugettext as _
from sitepal.sitemenu.actions.actions     import ACTION_DELETE_OBJECT
from sitepal.sitemenu.actions.object_view import ObjectView

class ObjectDeleteView(ObjectView):
    def __init__(self,model_class,post_delete_redirect,**kwargs):
        self._post_delete_redirect = post_delete_redirect
        ObjectView.__init__(self,model_class,**kwargs)

    def handle_get(self,request,**kwargs):
        obj     = self.get_object(request,**kwargs)
        context = self.get_context(request,obj)
        return self.response(request,context)

    def handle_post(self,request,**kwargs):
        obj = self.get_object(request,**kwargs)
        if self.has_perm(ACTION_DELETE_OBJECT,request.user,obj):
            msg = _("The {0} was deleted.").format(self._model_verbose_name)
            obj.delete()
            messages.success(request,msg,fail_silently=True)
            return self.post_delete_redirect(obj)
        else:
            return HttpResponseForbidden()

    def post_delete_redirect(self,obj):
        return self.redirect(self._post_delete_redirect,obj)

